package com.example.property.framgnet

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.common.mvi.BaseMviFragment
import com.example.property.R
import com.example.property.adapter.ParkApplyAdapter
import com.example.property.databinding.FragmentMineApplyBinding
import com.example.property.entity.ParkApplyValues
import com.example.property.intent.ParkApplyViewIntent
import com.example.property.uistate.ParkApplyUiState
import com.example.property.viewmodel.ParkApplyViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 *我的车位申请
 **/
@AndroidEntryPoint
class MineApplyFragment : BaseMviFragment<FragmentMineApplyBinding>() {

    private val mViewModel : ParkApplyViewModel by activityViewModels()

    private lateinit var parkApplyAdapter: ParkApplyAdapter
    companion object {
        @JvmStatic
        fun newInstance() = MineApplyFragment()
    }
    override fun getLayoutId(): Int=R.layout.fragment_mine_apply

    override fun initViews() {



    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(ParkApplyViewIntent.LoadParkApplyList)
    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is ParkApplyUiState.LoadParkApplySuccess->loadParkData(it.data as MutableList<ParkApplyValues>)
                is ParkApplyUiState.LoadParkApplyFail->showToastMessage("接口请求失败")
                else->{}
            }
        }
    }

    private fun loadParkData(values: MutableList<ParkApplyValues>) {
            parkApplyAdapter= ParkApplyAdapter(values)
            mDataBinding.rvParkApply.adapter=parkApplyAdapter
            mDataBinding.rvParkApply.layoutManager=LinearLayoutManager(requireContext())
    }

}